package com.itheima.threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/*
Executors  工具类
 */
public class Demo01 {
    public static void main(String[] args) throws InterruptedException {
        //使用工具类 创建默认线程池  (根据需要创建线程)
        ExecutorService pool = Executors.newCachedThreadPool();
        //有 线程池的功能
        pool.submit(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println("使用线程池1执行到了"+i);
                }
            }
        });
        pool.submit(()->{
            for (int i = 0; i < 100; i++) {
                System.out.println("使用线程池2执行到了"+i);
            }
        });
        new  Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println("使用自己创建的线程1执行到了"+i);
                }
            }
        }).start();

        new  Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println("用自己创建的线程2执行到了"+i);
                }
            }
        }).start();

        for (int i = 0; i < 100; i++) {
            System.out.println("main线程中执行到了"+i);
        }

        Thread.sleep(100);
        // 关闭线程池
        pool.shutdown();


    }
}
